package com.suda.sudaexamsystem.controller;

import com.suda.sudaexamsystem.dto.NoticeDto;
import com.suda.sudaexamsystem.pojo.Notice;
import com.suda.sudaexamsystem.pojo.User;
import com.suda.sudaexamsystem.service.NoticeService;
import com.suda.sudaexamsystem.service.UserService;
import com.suda.sudaexamsystem.util.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/notice")
@Slf4j
public class NoticeController {
    @Autowired
    private UserService userService;
    @Autowired
    private NoticeService noticeService;

    // 发布公告
    // userId 发布人员的id   userIds 哪些人可见 例如："1,2,3"  id为1，2，3的用户可以看见
    // notice需要title content publishTime格式 例如:2023-03-03 13:30:30 必须是这个格式
    @PostMapping(value = "/publish", produces = {"application/json;charset=UTF-8"})
    public AjaxResult publishNotice(Notice notice, int userId, String userIds) {
        log.info(userId + "");
        log.info(userIds);
        log.info(notice.toString());
        User user = userService.findById(userId);
        notice.setUser(user);
        List<User> userList = notice.getUserList();
        String[] parseStrs = userIds.trim().split(",");
        for (String s : parseStrs) {
            int id = Integer.parseInt(s.trim());
            User u = userService.findById(id);
            userList.add(u);
        }
        noticeService.publishNotice(notice);
        return AjaxResult.success();
    }

    // 删除公告
    @PostMapping(value = "/remove/{noticeId}", produces = {"application/json;charset=UTF-8"})
    public AjaxResult removeNotice(@PathVariable("noticeId") int noticeId) {
        noticeService.deleteById(noticeId);
        return AjaxResult.success();
    }

    // 查看公告
    // 返回的publish_time 统一都是时间戳 可以和日期对象直接转换
    /*
    {"msg":"操作成功","code":200,"data":{"id":1,"user":{"id":1,"name":"admin","login_name":"段智兴","sex":"男"},"publish_time":1701761424000,"title":"校考",
    "content":"软件项目管理于12月20日在泓远楼103，考试时间：15:00-17:30，请各位同学带好苏大通卡"}}
     */
    @GetMapping(value = "/{noticeId}", produces = {"application/json;charset=UTF-8"})
    public AjaxResult getNotice(@PathVariable("noticeId") int noticeId) {
        Notice notice = noticeService.findById(noticeId);
        NoticeDto noticeDto = new NoticeDto();
        noticeDto.setId(notice.getId());
        noticeDto.setUser(notice.getUser().toDto());
        noticeDto.setTitle(notice.getTitle());
        noticeDto.setContent(notice.getContent());
        noticeDto.setPublishTime(notice.getPublishTime());
        return AjaxResult.success(noticeDto);
    }

    // 查询用户的所有可见公告
    /*
    {"msg":"操作成功","code":200,"data":[{"id":1,"user":{"id":1,"name":"admin","login_name":"段智兴","sex":"男"},"publish_time":1701761424000,"title":"校考",
    "content":"软件项目管理于12月20日在泓远楼103，考试时间：15:00-17:30，请各位同学带好苏大通卡"}]}
     */
    @GetMapping(value = "/user/{userId}", produces = {"application/json;charset=UTF-8"})
    public AjaxResult getNotices(@PathVariable("userId") int userId) {
        List<Notice> noticeList = noticeService.findNoticesByUserId(userId);
        List<NoticeDto> noticeDtoList = new ArrayList<>();
        for (Notice notice : noticeList) {
            NoticeDto noticeDto = new NoticeDto();
            noticeDto.setId(notice.getId());
            noticeDto.setUser(notice.getUser().toDto());
            noticeDto.setTitle(notice.getTitle());
            noticeDto.setContent(notice.getContent());
            noticeDto.setPublishTime(notice.getPublishTime());
            noticeDtoList.add(noticeDto);
        }
        return AjaxResult.success(noticeDtoList);
    }
}
